#ifndef O_WINDOW_H
#define O_WINDOW_H

#include "OGlobal.h"

#include <OGRE/OgreRenderWindow.h>
#include <OGRE/OgreRoot.h>
#include <OGRE/OgreWindowEventUtilities.h>

#include "Core/OThread.h"
#include "Core/OSize.h"
#include "Core/OString.h"
#include "Core/OKeyEvent.h"
#include "Core/OMouseEvent.h"

O_START_NAMESPACE

class O_EXPORT Window : public Ogre::FrameListener, public Ogre::WindowEventListener, public Thread
{
private:
	Ogre::RenderWindow*			m_window;
	Ogre::Root*					m_root;
	int							m_close;
	bool						m_quit;

	static Window*				m_singleton;

public:
	Window(Size size, String title);
	virtual ~Window();

	void show();
	void close();

	void run()
	{
		while (!m_quit)
		{

		}
	}

	int exec()
	{
		m_singleton		= this;
		show();
		join();
		return m_close;
	}

	static void close(int code)
	{
		if (m_singleton != 0)
		{
			m_singleton->m_close	= code;
			m_singleton->m_quit		= true;
		}
	}

	Size size();
	String title();

	void windowMoved(Ogre::RenderWindow* rw);
	void windowResized(Ogre::RenderWindow* rw);
	bool windowClosing(Ogre::RenderWindow* rw);
	void windowClosed(Ogre::RenderWindow* rw);
	void windowFocusChange(Ogre::RenderWindow* rw);

	bool frameStarted(const Ogre::FrameEvent &evt);
	bool frameRenderingQueued(const Ogre::FrameEvent &evt);
	bool frameEnded(const Ogre::FrameEvent &evt);

	void keyPressed(KeyEvent *e);
	void keyReleased(KeyEvent *e);

	void mousePressed(MouseEvent *e);
	void mouseMoved(MouseEvent *e);
	void mouseReleased(MouseEvent *e);
};

O_END_NAMESPACE

#endif // O_WINDOW_H
